/***************************************************************************
 *   Copyright (C) 2009 by Jason Pullara                                   *
 *   lordkat@gmail.com                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "common.h"

using namespace std;

cFileIO::cFileIO() {
}

cFileIO::~cFileIO() {
}

bool cFileIO::fileExists(string sName) {
	fstream fFile;
	fFile.open(sName.c_str(), ios::in);
	if (!fFile.is_open())
		return false;
	fFile.close();
	return true;
}

bool cFileIO::characterExists(string sName) {
	string sFile = "data/players/";
	sName = sanitize(sName);
	sFile += sName;
	return this->fileExists(sFile);
}

sCharacterData cFileIO::characterLoad(string sName) {
	sCharacterData characterData;
	sName = sanitize(sName);
	string sFile = "data/players/";
	sFile += stripSpace(lowercase(sName));
	ifstream iFile(sFile.c_str(), ios::in|ios::binary);
	if (iFile.is_open()) {
		/* This data isn't saved so zero it out in case we allocate bad memory */
		characterData.ulMaxHP = 0;
		characterData.ulMaxMP = 0;
		characterData.ulMaxTP = 0;
		characterData.ulTNL = 0;
		characterData.uiStr = 0;
		characterData.uiDex = 0;
		characterData.uiVit = 0;
		characterData.uiAgi = 0;
		characterData.uiInt = 0;
		characterData.uiMnd = 0;
		characterData.uiChr = 0;
		characterData.uiAtk = 0;
		characterData.uiDef = 0;
		characterData.uiAcc = 0;
		characterData.uiEva = 0;
		characterData.uiMAc = 0;
		characterData.uiMEv = 0;
		characterData.uiStrMod = 0;
		characterData.uiDexMod = 0;
		characterData.uiVitMod = 0;
		characterData.uiAgiMod = 0;
		characterData.uiIntMod = 0;
		characterData.uiMndMod = 0;
		characterData.uiChrMod = 0;
		characterData.uiAtkMod = 0;
		characterData.uiDefMod = 0;
		characterData.uiAccMod = 0;
		characterData.uiEvaMod = 0;
		characterData.uiMAcMod = 0;
		characterData.uiMEvMod = 0;

		size_t sizeTemp;
		char *cName, *cPass, *cTitle;
		
		iFile.read((char *)&sizeTemp, sizeof(size_t));
		cName = new char[sizeTemp];
		iFile.read((char *)cName, sizeTemp);
		characterData.sName.resize(sizeTemp);
		characterData.sName.assign(cName, sizeTemp);
		
		iFile.read((char *)&sizeTemp, sizeof(size_t));
		cPass = new char[sizeTemp];
		iFile.read((char *)cPass, sizeTemp);
		characterData.sPassword.resize(sizeTemp);
		characterData.sPassword.assign(cPass, sizeTemp);

		iFile.read((char *)&sizeTemp, sizeof(size_t));
		cTitle = new char[sizeTemp];
		iFile.read((char *)cTitle, sizeTemp);
		characterData.sTitle.resize(sizeTemp);
		characterData.sTitle.assign(cTitle, sizeTemp);

		free(cName);
		free(cPass);
		free(cTitle);

		iFile.read((char *)&characterData.ucJob, sizeof(unsigned char));
		iFile.read((char *)&characterData.ucSubJob, sizeof(unsigned char));
		iFile.read((char *)&characterData.ucRace, sizeof(unsigned char));
		for (int i = 0; i < MAX_JOBS; i++)
			iFile.read((char *)&characterData.uiLevel[i], sizeof(unsigned int));
		for (int i = 0; i < MAX_JOBS; i++)
			iFile.read((char *)&characterData.ulExp[i], sizeof(unsigned long));
		iFile.read((char *)&characterData.ulGold, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulMap, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulHP, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulMP, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulTP, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqHead, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqNeck1, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqNeck2, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqEar1, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqEar2, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqBack, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqChest, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqArms, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqHands, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqWaist1, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqWaist2, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqLegs, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqFeet, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqHold1, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqHold2, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqRangedWeapon, sizeof(unsigned long));
		iFile.read((char *)&characterData.ulEqRangedAmmo, sizeof(unsigned long));
		iFile.close();
	}
	return characterData;
}

bool cFileIO::characterSave(sCharacterData characterData) {
	string sName = sanitize(characterData.sName);
	string sFile = "data/players/";
	sFile += stripSpace(lowercase(sName));
	ofstream oFile(sFile.c_str(), ios::out|ios::binary);
	if (oFile.is_open()) {
		size_t sizeTemp = characterData.sName.length();
		char *cName = (char *)characterData.sName.c_str();
		oFile.write((char *)&sizeTemp, sizeof(size_t));
		oFile.write((char *)cName, sizeTemp);
		sizeTemp = characterData.sPassword.length();
		char *cPass = (char *)characterData.sPassword.c_str();
		oFile.write((char *)&sizeTemp, sizeof(size_t));
		oFile.write((char *)cPass, sizeTemp);
		sizeTemp = characterData.sTitle.length();
		char *cTitle = (char *)characterData.sTitle.c_str();
		oFile.write((char *)&sizeTemp, sizeof(size_t));
		oFile.write((char *)cTitle, sizeTemp);
		oFile.write((char *)&characterData.ucJob, sizeof(unsigned char));
		oFile.write((char *)&characterData.ucSubJob, sizeof(unsigned char));
		oFile.write((char *)&characterData.ucRace, sizeof(unsigned char));
		for (int i = 0; i < MAX_JOBS; i++)
			oFile.write((char *)&characterData.uiLevel[i], sizeof(unsigned int));
		for (int i = 0; i < MAX_JOBS; i++)
			oFile.write((char *)&characterData.ulExp[i], sizeof(unsigned long));
		oFile.write((char *)&characterData.ulGold, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulMap, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulHP, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulMP, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulTP, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqHead, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqNeck1, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqNeck2, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqEar1, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqEar2, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqBack, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqChest, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqArms, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqHands, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqWaist1, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqWaist2, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqLegs, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqFeet, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqHold1, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqHold2, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqRangedWeapon, sizeof(unsigned long));
		oFile.write((char *)&characterData.ulEqRangedAmmo, sizeof(unsigned long));
		oFile.close();
	}
	else
		return false;
	return true;
}
